package cn.czh.controller;

import cn.czh.domain.Middle;
import cn.czh.domain.Movie;
import cn.czh.service.IMovieService;
import cn.czh.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class MovieController {
    @Autowired
    private IMovieService movieService;


    /**
     * 查询所有
     *
     * @return
     */
    @GetMapping("/movie/findAll")
    public List<Movie> findAll() {
        return movieService.findAll();
    }

    /**
     * 查询单个
     *
     * @param id
     * @return
     */
    @GetMapping("/movie/getById/{id}")
    public Movie getById(@PathVariable("id") Long id) {
        return movieService.getById(id);
    }

    /**
     * 随机赠送一张电影票
     *
     * @return
     */
    @GetMapping("/movie/random")
    public Movie random() {
        return movieService.random();
    }


    /**
     * 新增或修改电影信息
     *
     * @param movie
     * @return
     */
    @PostMapping("/movie/addOrUpdate")
    public AjaxResult addOrUpdateMt(@RequestBody Movie movie) {
        try {
            if (movie.getId() == null) { //如果电影票为空
                movieService.addMt(movie);
            } else {
                movieService.updateMt(movie);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "操作失败");
        }
    }


    /**
     * 购买电影票
     *
     * @param middle
     * @return
     */
    @PostMapping("/movie/buy")
    public AjaxResult buyTicket(@RequestBody Middle middle) {
        try {
            movieService.buy(middle);
            return new AjaxResult();
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "操作失败");
        }
    }

    /**
     * 观看电影
     * @param middle
     * @return
     */
    @PostMapping("/movie/see")
    public AjaxResult seeMovie(@RequestBody Middle middle) {
        try {
            movieService.seeMovie(middle);
            return new AjaxResult();
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }
}
